package com.woniuxy.mesboot.controller;

import com.woniuxy.mesboot.dto.UserDTO;
import com.woniuxy.mesboot.service.UserService;
import com.woniuxy.mesboot.util.ObjectExcelView;
import com.woniuxy.mesboot.util.PageData;
import com.woniuxy.mesboot.vo.UserVO;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：lisir
 * @date ：Created in 2023/5/29 12:11
 * @description：导入导出数据
 */
@RestController
@RequestMapping("/excel")
public class ExcelController {

    @Resource
    private UserService userService;

    @RequestMapping(value = "/exportExcel")
    public ModelAndView exportExcel(@RequestBody UserDTO user) {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            //构造列名称
            List<String> titles = new ArrayList<>();
            titles.add("用户编号"); // 1
            titles.add("用户名"); // 2
            titles.add("昵称"); // 3
            titles.add("工号"); // 4
            titles.add("电话"); // 5
            titles.add("生日"); // 6
            dataMap.put("titles", titles);
            //得到被导出的数据
            List<UserVO> varOList = userService.selectAll(user);
            List<PageData> varList = new ArrayList<>();
            for (UserVO userVO : varOList) {
                PageData vpd = new PageData();
                vpd.put("var1", userVO.getUserId());
                vpd.put("var2", userVO.getUserName());
                vpd.put("var3", userVO.getNickName());
                vpd.put("var4", userVO.getWorkNo());
                vpd.put("var5", userVO.getTel());
                vpd.put("var6", userVO.getCreatTime());
                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }
}
